#!/usr/bin/env python

from news_class import News
from liwe.decorators import dataset

def get ( liwe, id_news = 0, permalink = '', affinity = 0, template_name = 'NEWS_SHOW' ):
	"""
	:INPUT:
		- id_news:              SAFE_NUMBER
		- permalink:		SAFE_DB_STRING
		- affinity:		SAFE_BOOL
		- template_name:	SAFE_DB_STRING
	"""

	n = News ( liwe )

	res = { 'news' : n.get ( id_news = id_news, permalink = permalink, template_name = template_name ) }

	if affinity:
		res [ 'affinity' ] = n.get_affinity ( id_news )

	return res

@dataset
def search ( liwe, testo = '', pub_date = None, id_categs = '', tags = '', exclude_tags = '', limit = 10, start_point = 0 ): #, _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:

		- testo:		SAFE_STRING
		- pub_date:             SAFE_DATE
		- tags:			SAFE_STRING
		- exclude_tags:		SAFE_STRING
		- id_categs:		SAFE_STRING
		- limit:	SAFE_NUMBER
		- start_point:  SAFE_NUMBER
	"""

	n = News ( liwe )
	res = n.search ( testo = testo, id_categs = id_categs, tags = tags, exclude_tags = exclude_tags, start_pos = start_point, limit = limit, ds_sort = '' )

	for v in res [ 'rows' ]:
		try:
			media = v [ 'media' ] [ 0 ]
			if media and len ( media ) > 0:
				v [ '_img' ] = media [ 'html' ] % { "_size" : "icon", "_ext" :  '.jpg' }
		except:
			v [ '_img' ] = '-1'

	return res

def _create_dataset ( start_point, lines, rows, tot_rows ):
	res = {}

	count = 0
	for v in rows:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v
		count += 1
		

	res [ 'rows' ] =  tot_rows
	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = count # lines

	return res


@dataset
def list ( liwe, mode = "latest", id_categs = '', limit = 3, tags = '', exclude_tags = "", exclude_ids = "", start_point = 0 ):
	"""
	:INPUT:
		- mode:		SAFE_STRING
		- id_categs:	SAFE_STRING
		- tags:		SAFE_STRING
		- exclude_tags: SAFE_STRING
		- exclude_ids:  SAFE_STRING
		- limit:	SAFE_NUMBER
		- start_point:  SAFE_NUMBER
	"""
	n = News ( liwe )
	res = n.list ( mode = mode, id_categs = id_categs, quant = limit, tags = tags, exclude_tags = exclude_tags, exclude_ids = exclude_ids, start_point = start_point, do_count = 1 )

	#rows = ret [ 'rows' ]

	for row in res [ 'rows' ]:
		# FIXME: fare in modo che se non ci sono immagini
		#	 metta una grafica di default
		try:
			media = row [ 'media' ] [ 0 ]
		except:
			media = { "html" : "Immagine non disp", "ext" : "xxx" } 

		row [ '_img' ] = media [ 'html' ] % { "_size" : "icon", "_ext" :  '.jpg'  }

	return res

def get_categ ( liwe, id_categ):
	"""
	:INPUT:
		- id_categ:              SAFE_NUMBER
	"""

	n = News ( liwe )
	return { 'categ': n.get_categ ( id_categ ) }



def list_categs ( liwe, id_node = None ):
	"""
	:INPUT:
		- id_news:              SAFE_NUMBER
		- id_node:              SAFE_NUMBER
	"""

	n = News ( liwe )

	rs = n.list_categs ( id_node )
	if not rs: rs = []

	return { 'categs': rs }

def get_templates ( liwe ):
	"""
        :INPUT:
	"""
	n = News ( liwe )
	return { "templates" : n.get_templates () }

def sitemap ( liwe ):
	n = News ( liwe )
	return { "sitemap" : n.sitemap () }
